<?xml version='1.0' encoding='UTF-8' ?>
<html xmlns="http://www.w3.org/1999/xhtml"
	xmlns:h="http://java.sun.com/jsf/html"
	xmlns:f="http://java.sun.com/jsf/core"
	xmlns:ui="http://java.sun.com/jsf/facelets"
	xmlns:p="http://primefaces.org/ui">
	
<f:view encoding="ISO-8859-1">
	<h:head>
		<title>Clientes cadastrados</title>
		<h:outputStylesheet library="css" name="styles.css" />
		<h:outputScript library="js" name="scripts.js" />
		<h:outputScript library="js" name="md5.js" />
		<script type="text/javascript">
			function validarForm() {
				if (!validaCpf(document.getElementById('frmClient:txtClientCPF').value)) {
					alert("CPF inválido. Tente novamente.");
					return false;
				} else {
					return true;
				}
			}
		</script>
	</h:head>
	<h:body>
		<h:form id="frmClient" styleClass="frm"
			rendered="#{loginBean.isLogged() and loginBean.hasPermission('CadGeneral')}">
			
			<ui:include src="../../templates/menu.xhtml" />
			
			<p:growl id="messages" showDetail="true" sticky="true"/>
				
			<p:panel id="cadPanelClient" header="Cadastro de clientes" styleClass="cadPanelPrincipal">
				<h:panelGrid id="panelGrid" columns="6" cellspacing="5">
					<h:outputLabel id="lblClientName" value="Nome: "
						styleClass="outPutLabel" />
					<p:inputText id="txtClientName" label="Nome"
						styleClass="inputText" value="#{clientBean.name}">
					</p:inputText>
					
					<h:outputLabel id="lblClientCPF" value="CPF: "
						styleClass="outPutLabel" />
					<p:inputMask id="txtClientCPF" label="CPF" styleClass="inputText" mask="999.999.999-99"
						value="#{clientBean.cpf}">
					</p:inputMask>
					
					<h:outputLabel id="lblClientBirth" value="Data de nascimento: "
						styleClass="outPutLabel" />
					<p:calendar locale="pt_BR" showButtonPanel="true" navigator="true" label="Data de nascimento"
						value="#{clientBean.birthDate}" pattern="dd/MM/yyyy" mindate="01/01/1900"/>
					
					<h:outputLabel id="lblClientEmail" value="E-mail: "
						styleClass="outPutLabel" />
					<p:inputText id="txtClientEmail" label="E-mail"
						styleClass="inputText" value="#{clientBean.email}">
					</p:inputText>
						
					<h:outputLabel id="lblClientPhone1" value="Telefone: "
						styleClass="outPutLabel" />
					<p:inputMask id="txtClientPhone1" label="Telefone" styleClass="inputText" mask="(99)9999-9999"
						value="#{clientBean.phone1}">
					</p:inputMask>

					<h:outputLabel id="lblClientPhone2" value="Celular: "
						styleClass="outPutLabel" />
					<p:inputMask id="txtClientPhone2" label="Celular" styleClass="inputText" mask="(99)9999-9999"
						value="#{clientBean.phone2}">
					</p:inputMask>
							
					<h:outputLabel id="lblClientCEP" value="CEP: "
						styleClass="outPutLabel" />
					<p:inputMask id="txtClientCEP" label="CEP" styleClass="inputText" mask="99999-999"
						value="#{addressBean.zip}">
						<f:ajax event="blur" listener="#{addressBean.loadAddresses}"
							render="txtClientEstate txtClientCity txtClientNeighborhood selClientStreet" />
					</p:inputMask>
					<h:outputLabel id="lblClientEstate" value="Estado: "
						styleClass="outPutLabel" />
					<p:inputText id="txtClientEstate" label="Estado"
						styleClass="inputText" value="#{addressBean.estate.name}">
					</p:inputText>
					<h:outputLabel id="lblClientCity" value="Cidade: "
						styleClass="outPutLabel" />
					<p:inputText id="txtClientCity" label="Cidade" styleClass="inputText"
						value="#{addressBean.city.name}">
					</p:inputText>
					<h:outputLabel id="lblClientNeighborhood" value="Bairro: "
						styleClass="outPutLabel" />
					<p:inputText id="txtClientNeighborhood" label="Bairro"
						styleClass="inputText"
						value="#{addressBean.neighborhood.name}">
					</p:inputText>
					<h:outputLabel id="lblClientStreet" value="Rua: "
						styleClass="outPutLabel" />
					<p:selectOneMenu id="selClientStreet" value="#{clientBean.addCode}">
						<f:selectItem itemLabel="Selecione o logradouro" itemValue="" />  
						<f:selectItems value="#{addressBean.addressList}"  var="addressItem"
							itemValue="#{addressItem.adcode}" itemLabel="#{addressItem.name}" />
						<p:ajax listener="#{clientBean.selAddressChanged}"/>
					</p:selectOneMenu>
					<h:outputLabel id="lblClientAddressNumber" value="Número: "
						styleClass="outPutLabel" />
					<p:inputText id="txtClientAddressNumber" label="Número"
						styleClass="inputText"
						value="#{clientBean.addNumber}">
					</p:inputText>
					<p:commandButton id="cmdButtonRegister" value="Cadastrar" onclick="return validarForm();" actionListener="#{clientBean.onRegisterClick}" update=":frmClient:tblClients :frmClient:messages" icon="ui-icon-disk"/>
				</h:panelGrid>
				<p:dataTable id="tblClients" var="cli" value="#{clientBean.clientList}" rowKey="#{cli.clcode}"	selection="#{clientBean.client}" selectionMode="single">
				
					<p:column headerText="NOME">
						#{cli.person.name}
					</p:column>
					<p:column headerText="CPF">
						#{cli.formattedDocument}
					</p:column>
					<p:column headerText="E-MAIL">
						#{cli.person.email}
					</p:column>
					<p:column headerText="TELEFONE">
						#{cli.person.phone1}
					</p:column>
					<p:column headerText="CELULAR">
						#{cli.person.phone2}
					</p:column>
					
					<f:facet name="footer">
						<p:commandButton id="updButton" value="Atualizar" icon="ui-icon-pencil"/>
					</f:facet>
				
				</p:dataTable>
			</p:panel>
		</h:form>
		<h:form	rendered="#{!loginBean.isLogged() or !loginBean.hasPermission('CadGeneral')}">
			<h1 align="center">
				<h:outputText value="Acesso não autorizado a esta página!" />
			</h1>
			<h1 align="center">
				<h:graphicImage value="#{resource['images/general/forbidden.png']}" />
			</h1>
		</h:form>
		<ui:include src="../../templates/footer.xhtml" />
	</h:body>
</f:view>
</html>